<?php

namespace App\Http\Controllers;

use App\Utils\Code;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 后台来路
     * @var bool
     */
    protected $manager = false;

    /**
     * 响应类型
     * @var string
     */
    protected $contentType = 'json';

    /**
     * 响应异常处理
     * @param string $method
     * @param array $parameters
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters)
    {
        // TODO: Change the autogenerated stub
        try {
            $this->manager = request()->get('source') == 'manager';
            $response = parent::callAction($method, $parameters);
            if ($this->contentType == 'json'){
                return Code::response($response);
            }
            return $response;
        } catch (\Throwable $exception) {
            if ($this->contentType == 'json'){
                return Code::exception($exception);
            }
            return response($exception->getMessage())->header('Content-Type', 'text/html; charset=UTF-8');
        }
    }
}
